//
//  OrderDetailsModel.swift
//  Pods
//
//  Created by eme on 2017/4/1.
//
//

import Foundation
import SwiftyJSON
import NetWorkCore
import ModelProtocol


class OrderDetailsModel: ModelProtocol {
    
    // MARK: Declaration for string constants to be used to decode and also serialize.
    internal let kOrderDetailsInvoiceflagKey: String = "invoiceflag"
    internal let kOrderDetailsOrderDetailsArrayKey: String = "array"
    internal let kOrderDetailsAdressKey: String = "adress"
    internal let kOrderDetailsPayidKey: String = "payid"
    internal let kOrderDetailsOrderstatusKey: String = "orderstatus"
    internal let kOrderDetailsCommentKey: String = "comment"
    internal let kOrderDetailsIsshowKey: String = "isshow"
    internal let kOrderDetailsOrderidKey: String = "orderid"
    internal let kOrderDetailsProvinceKey: String = "province"
    internal let kOrderDetailsIsinvoiceKey: String = "isinvoice"
    internal let kOrderDetailsSendtypeKey: String = "sendtype"
    internal let kOrderDetailsInvoicetitleKey: String = "invoicetitle"
    internal let kOrderDetailsCityKey: String = "city"
    internal let kOrderDetailsIsevaluateKey: String = "isevaluate"
    internal let kOrderDetailsUnderdateKey: String = "underdate"
    internal let kOrderDetailsBdidKey: String = "bdid"
    internal let kOrderDetailsSpidKey: String = "spid"
    internal let kOrderDetailsAreaKey: String = "area"
    internal let kOrderDetailsCellphoneKey: String = "cellphone"
    internal let kOrderDetailsInvoiceplaceKey: String = "invoiceplace"
    internal let kOrderDetailsPostageKey: String = "postage"
    internal let kOrderDetailsSelfidKey: String = "selfid"
    internal let kOrderDetailsOrderamountKey: String = "orderamount"
    internal let kOrderDetailsAgidKey: String = "agid"
    internal let kOrderDetailsRecipientsKey: String = "recipients"
    internal let kOrderDetailsCloseorderdateKey: String = "closeorderdate"
    internal let kOrderDetailsbdnameKey:String = "bdname"
    internal let kOrderDetailstotalnumberKey:String = "totalnumber"
    internal let kOrderDetailsSendtimeKey:String = "sendtime"//发货时间
    internal let kOrderDetailsPaydateKey:String = "paydate"//支付时间
    internal let kOrderDetailsTaketimeKey:String = "taketime"//收货时间
    internal let kOrderDetailsCanceltimeKey:String = "canceltime"//取消时间
    internal let kOrderDetailsReceivegoodsdateKey:String = "receivegoodsdate"//自动确认收货时间
    internal let kOrderDetailsissueaninvoiceKey:String = "issueaninvoice"//是否显示发票抬头
    internal let kLogisticsKuaidiKey: String = "kuaidi"
    internal let kLogisticsPaycouponKey:String = "paycoupon"
    internal let kLogisticspaycouponidKey:String = "paycouponid"
    internal let kTheOrderListpayarraykey:String = "payarray"
    internal let kOrderDetailsAgmobileKey:String = "agmobile"
    internal let kOrderDetailsreCeivegoodslefttimeKey:String = "receivegoodslefttime"
    internal let kOrderCloseorderlefttimeKey:String = "closeorderlefttime"
    internal let kOrderAgnameKey:String = "agname"
    
    
    // MARK: 属性
    var invoiceflag: String
    var orderDetailsArray: [OrderDetailsArrayModel]
    var adress: String
    var payid: String
    var orderstatus: String
    var comment: String
    var isshow: String
    var orderid: String
    var province: String
    var isinvoice: String
    var sendtype: String
    var invoicetitle: String
    var city: String
    var isevaluate: String
    var underdate: String
    var bdid: String
    var spid: String
    var area: String
    var cellphone: String
    var invoiceplace: String
    var postage: Double
    var selfid: String
    var orderamount: Double
    var agid: String
    var recipients: String
    var closeorderdate:String
    var bdname:String
    var totalnumber:String
    var sendtime:String
    var paydate:String
    var taketime:String
    var canceltime:String
    var receivegoodsdate:String
    var issueaninvoice:String
    var kuaidi:KuaidiModel
    var paycoupon:Double
    var paycouponid:String
    var payarray:[PayArrayModel]
    var agmobile:String
    var receivegoodslefttime:Int64
    var closeorderlefttime:Int64
    var agname:String
    
    // MARK: 实现MikerSwiftJSONAble 协议， 解析json数据
    required  init?(json: JSON) {
        invoiceflag = json[kOrderDetailsInvoiceflagKey].stringValue
        orderDetailsArray = []
        if let items = json[kOrderDetailsOrderDetailsArrayKey].array {
            for item in items {
                if let orderDetailsArrayModel = OrderDetailsArrayModel(json: item){
                    orderDetailsArray.append(orderDetailsArrayModel)
                }
            }
        }else{
            orderDetailsArray = []
        }
        closeorderlefttime = json[kOrderCloseorderlefttimeKey].int64Value
        receivegoodslefttime = json[kOrderDetailsreCeivegoodslefttimeKey].int64Value
        adress = json[kOrderDetailsAdressKey].stringValue
        payid = json[kOrderDetailsPayidKey].stringValue
        orderstatus = json[kOrderDetailsOrderstatusKey].stringValue
        comment = json[kOrderDetailsCommentKey].stringValue
        isshow = json[kOrderDetailsIsshowKey].stringValue
        orderid = json[kOrderDetailsOrderidKey].stringValue
        province = json[kOrderDetailsProvinceKey].stringValue
        isinvoice = json[kOrderDetailsIsinvoiceKey].stringValue
        sendtype = json[kOrderDetailsSendtypeKey].stringValue
        invoicetitle = json[kOrderDetailsInvoicetitleKey].stringValue
        city = json[kOrderDetailsCityKey].stringValue
        isevaluate = json[kOrderDetailsIsevaluateKey].stringValue
        underdate = json[kOrderDetailsUnderdateKey].stringValue
        bdid = json[kOrderDetailsBdidKey].stringValue
        spid = json[kOrderDetailsSpidKey].stringValue
        area = json[kOrderDetailsAreaKey].stringValue
        cellphone = json[kOrderDetailsCellphoneKey].stringValue
        invoiceplace = json[kOrderDetailsInvoiceplaceKey].stringValue
        postage = json[kOrderDetailsPostageKey].doubleValue
        selfid = json[kOrderDetailsSelfidKey].stringValue
        orderamount = json[kOrderDetailsOrderamountKey].doubleValue
        agid = json[kOrderDetailsAgidKey].stringValue
        recipients = json[kOrderDetailsRecipientsKey].stringValue
        closeorderdate = json[kOrderDetailsCloseorderdateKey].stringValue
        bdname = json[kOrderDetailsbdnameKey].stringValue
        totalnumber = json[kOrderDetailstotalnumberKey].stringValue
        sendtime = json[kOrderDetailsSendtimeKey].stringValue
        paydate = json[kOrderDetailsPaydateKey].stringValue
        taketime = json[kOrderDetailsTaketimeKey].stringValue
        canceltime = json[kOrderDetailsCanceltimeKey].stringValue
        receivegoodsdate = json[kOrderDetailsReceivegoodsdateKey].stringValue
        issueaninvoice = json[kOrderDetailsissueaninvoiceKey].stringValue
        paycoupon = json[kLogisticsPaycouponKey].doubleValue
        kuaidi = KuaidiModel(json: json[kLogisticsKuaidiKey])!
        paycouponid = json[kLogisticspaycouponidKey].stringValue
        agmobile = json[kOrderDetailsAgmobileKey].stringValue
        agname = json[kOrderAgnameKey].stringValue
        payarray = []
        if let items = json[kTheOrderListpayarraykey].array {
            for item in items {
                if let arrayModel = PayArrayModel(json: item){
                    payarray.append(arrayModel)
                }
            }
        } else {
            payarray = []
        }
    }
    
    
    /**
     Generates description of the object in the form of a NSDictionary.
     - returns: A Key value pair containing all valid values in the object.
     */
    internal func dictionaryRepresentation() -> [String : AnyObject ] {
        
        var dictionary: [String : AnyObject ] = [ : ]
        
        dictionary.updateValue(invoiceflag as AnyObject, forKey: kOrderDetailsInvoiceflagKey)
        
        if orderDetailsArray.count > 0 {
            var temp: [AnyObject] = []
            for item in orderDetailsArray {
                temp.append(item.dictionaryRepresentation() as AnyObject)
            }
            dictionary.updateValue(temp as AnyObject, forKey: kOrderDetailsOrderDetailsArrayKey)
        }
        
        dictionary.updateValue(adress as AnyObject, forKey: kOrderDetailsAdressKey)
    
        dictionary.updateValue(payid as AnyObject, forKey: kOrderDetailsPayidKey)
    
        dictionary.updateValue(orderstatus as AnyObject, forKey: kOrderDetailsOrderstatusKey)
    
        dictionary.updateValue(comment as AnyObject, forKey: kOrderDetailsCommentKey)
    
        dictionary.updateValue(isshow as AnyObject, forKey: kOrderDetailsIsshowKey)
    
        dictionary.updateValue(orderid as AnyObject, forKey: kOrderDetailsOrderidKey)
    
        dictionary.updateValue(province as AnyObject, forKey: kOrderDetailsProvinceKey)
    
        dictionary.updateValue(isinvoice as AnyObject, forKey: kOrderDetailsIsinvoiceKey)
    
        dictionary.updateValue(sendtype as AnyObject, forKey: kOrderDetailsSendtypeKey)
    
        dictionary.updateValue(invoicetitle as AnyObject, forKey: kOrderDetailsInvoicetitleKey)
    
        dictionary.updateValue(city as AnyObject, forKey: kOrderDetailsCityKey)
    
        dictionary.updateValue(isevaluate as AnyObject, forKey: kOrderDetailsIsevaluateKey)
    
        dictionary.updateValue(underdate as AnyObject, forKey: kOrderDetailsUnderdateKey)
    
        dictionary.updateValue(bdid as AnyObject, forKey: kOrderDetailsBdidKey)
    
        dictionary.updateValue(spid as AnyObject, forKey: kOrderDetailsSpidKey)
    
        dictionary.updateValue(area as AnyObject, forKey: kOrderDetailsAreaKey)
    
        dictionary.updateValue(cellphone as AnyObject, forKey: kOrderDetailsCellphoneKey)
    
        dictionary.updateValue(invoiceplace as AnyObject, forKey: kOrderDetailsInvoiceplaceKey)
    
        dictionary.updateValue(postage as AnyObject, forKey: kOrderDetailsPostageKey)
    
        dictionary.updateValue(selfid as AnyObject, forKey: kOrderDetailsSelfidKey)
    
        dictionary.updateValue(orderamount as AnyObject, forKey: kOrderDetailsOrderamountKey)
    
        dictionary.updateValue(agid as AnyObject, forKey: kOrderDetailsAgidKey)
    
        dictionary.updateValue(recipients as AnyObject, forKey: kOrderDetailsRecipientsKey)
    
        dictionary.updateValue(closeorderdate as AnyObject, forKey: kOrderDetailsCloseorderdateKey)
    
        dictionary.updateValue(bdname as AnyObject, forKey: kOrderDetailsbdnameKey)
    
        dictionary.updateValue(totalnumber as AnyObject, forKey: kOrderDetailstotalnumberKey)
    
        dictionary.updateValue(sendtime as AnyObject, forKey: kOrderDetailsSendtimeKey)
    
        dictionary.updateValue(paydate as AnyObject, forKey: kOrderDetailsPaydateKey)
    
        dictionary.updateValue(taketime as AnyObject, forKey: kOrderDetailsTaketimeKey)
    
        dictionary.updateValue(canceltime as AnyObject, forKey: kOrderDetailsCanceltimeKey)
    
        dictionary.updateValue(receivegoodsdate as AnyObject, forKey: kOrderDetailsReceivegoodsdateKey)
        
        dictionary.updateValue(issueaninvoice as AnyObject, forKey: kOrderDetailsissueaninvoiceKey)
    
        dictionary.updateValue(kuaidi.dictionaryRepresentation() as AnyObject, forKey: kLogisticsKuaidiKey)
    
        dictionary.updateValue(paycoupon as AnyObject, forKey: kLogisticsPaycouponKey)
    
        dictionary.updateValue(paycouponid as AnyObject, forKey: kLogisticspaycouponidKey)
    
        dictionary.updateValue(agname as AnyObject, forKey: kOrderAgnameKey)
        
        if payarray.count > 0 {
            var temp: [AnyObject] = []
            for item in payarray {
                temp.append(item.dictionaryRepresentation() as AnyObject)
            }
            dictionary.updateValue(temp as AnyObject, forKey: kTheOrderListpayarraykey)
        }
        
        dictionary.updateValue(agmobile as AnyObject, forKey: kOrderDetailsAgmobileKey)
        
        return dictionary
    }
    
    
}
